package com.wltt.product.ahuthr.model;

import lombok.Setter;

import java.io.Serializable;

@Setter
public class ResultData<T> implements Serializable {

    private static final int SUCCESS_CODE = 0;
    private static final int FAILURE_CODE = 1;

    private static final String SUCCESS_MESSAGE = "操作成功";
    private static final String FAILURE_MESSAGE = "操作失败";

    /**
     * 返回状错误码,0标识成功
     */
    private int code;

    /**
     * 操作结果
     */
    private boolean success;

    /**
     * 返回的消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    private ResultData() {
    }

    private ResultData(Integer code, boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
    }

    private ResultData(T data) {
        this.code = ResultData.SUCCESS_CODE;
        this.success = true;
        this.message = ResultData.SUCCESS_MESSAGE;
        this.data = data;
    }

    public static ResultData success() {
        return new ResultData(ResultData.SUCCESS_CODE, true, ResultData.SUCCESS_MESSAGE);
    }

    public static ResultData success(Object result) {
        return new ResultData(result);
    }

    public static ResultData failure() {
        return new ResultData(ResultData.FAILURE_CODE, false, ResultData.FAILURE_MESSAGE);
    }

    public static ResultData failure(String respDesc) {
        return new ResultData(ResultData.FAILURE_CODE, false, respDesc);
    }

    public static ResultData failure(int respCode, String respDesc) {
        return new ResultData(respCode, false, respDesc);
    }

    public Integer getCode() {
        return code;
    }

    public boolean getSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return "ResultData{" +
                "code=" + code +
                ", success=" + success +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
